package com.zxm.sunshineaiary.view.myedittext;

import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.view.animation.Animation.AnimationListener;
import android.widget.FrameLayout;

import com.zxm.sunshineaiary.activity.R;

public class PaintColorLayout extends FrameLayout implements OnTouchListener,
		AnimationListener {

	public interface OnPaintColorListener {
		public void paintColor(int color);
	}

	private String TAG = "PaintColorLayout";

	private Context mContext = null;
	private OnPaintColorListener mOnPaintColorListener = null;
	private Animation mShowAnimation = null;
	private Animation mCloseAnimation = null;
	private MyRect[] myRects = null;

	private int defaultSelect = 3;
	private int viewNum = 0;
	private int pandding = 0;
	private int row = 0;
	private int mTopMargin = 0;
	private int mLeftMargin = 0;

	private boolean defaultColorSelect = true;
	private int[] colors = null;

	public PaintColorLayout(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		intit(context);
		getPropertyOfXml(attrs);
		createColor();
		createAnimation();
	}

	public PaintColorLayout(Context context, AttributeSet attrs) {
		super(context, attrs);
		intit(context);
		getPropertyOfXml(attrs);
		createColor();
		createAnimation();
	}

	public PaintColorLayout(Context context) {
		super(context);

	}

	private void intit(Context context) {
		this.mContext = context;
	}

	private void getPropertyOfXml(AttributeSet attrs) {

		TypedArray ta = mContext.obtainStyledAttributes(attrs,
				R.styleable.PaintColorLayout);
		viewNum = (int) ta.getFloat(R.styleable.PaintColorLayout_view_num, 0);
		row = (int) ta.getFloat(R.styleable.PaintColorLayout_view_row, 0);
		pandding = (int) ta.getDimension(
				R.styleable.PaintColorLayout_view_panding, 0);
		colors = new int[viewNum];
		myRects = new MyRect[viewNum];

		mTopMargin = pandding;
		mLeftMargin = pandding;
	}

	/**
	 * 颜色的数量要和ChildView数量对应上
	 */
	private void createColor() {
		colors[0] = this.getResources().getColor(R.color.baby_pink);
		colors[1] = this.getResources().getColor(R.color.blue);
		colors[2] = this.getResources().getColor(R.color.green);
		colors[3] = R.color.black;
	}

	@Override
	protected void onSizeChanged(int w, int h, int oldw, int oldh) {
		int viewWidth = (w - viewNum * pandding) / row;
		int viewhight = (h - viewNum * pandding) / (viewNum / row);

		for (int i = 0; i < viewNum; i++) {

			myRects[i] = createChildView(colors[i]);
			myRects[i].setOnTouchListener(this);
			addView(myRects[i], viewWidth, viewhight);
			mLeftMargin += (viewWidth + pandding);
			if (i == (row - 1)) {
				mTopMargin += (viewhight + pandding);
				mLeftMargin = pandding;
			}
		}
		super.onSizeChanged(w, h, oldw, oldh);

	}

	private void addView(MyRect myRect, int viewWidth, int viewhight) {
		FrameLayout.LayoutParams lp = new FrameLayout.LayoutParams(viewWidth,
				viewhight);
		lp.gravity = Gravity.TOP;
		lp.topMargin = mTopMargin;
		lp.leftMargin = mLeftMargin;
		this.addView(myRect, lp);
	}

	private void createAnimation() {
		mShowAnimation = AnimationUtils.loadAnimation(mContext,
				R.anim.fingerpaint_show);
		mCloseAnimation = AnimationUtils.loadAnimation(mContext,
				R.anim.fingerpaint_close);
		mShowAnimation.setAnimationListener(this);
		mCloseAnimation.setAnimationListener(this);
	}

	private MyRect createChildView(int color) {
		MyRect myRect = new MyRect(mContext, color);
		return myRect;
	}

	@Override
	public boolean onTouch(View v, MotionEvent event) {
		if (event.getAction() == MotionEvent.ACTION_UP
				&& null != mOnPaintColorListener) {
			MyRect myRect = (MyRect) v;
			isTouchView(myRect);
			mOnPaintColorListener.paintColor(myRect.getViewColor());

		}
		return false;
	}

	/**
	 * 判断并给选中的View变色
	 * 
	 * @param myRect
	 */
	private void isTouchView(MyRect myRect) {
		for (MyRect my : myRects) {
			if (my == myRect) {
				my.selectOn();
			} else {
				my.selectOf();
			}
		}
	}

	/**
	 * 设置默认选中的画笔颜色
	 * 
	 * @param select
	 */
	private void setDefaultSelect(int select) {
		myRects[select].selectOn();
		mOnPaintColorListener.paintColor(myRects[select].getViewColor());
	}

	/**
	 * 设置颜色选择监听事件
	 * 
	 * @param onPaintColorListener
	 */
	public void setOnPaintColorListener(
			OnPaintColorListener onPaintColorListener) {
		mOnPaintColorListener = onPaintColorListener;
	}

	public void showLayout() {
		this.startAnimation(mShowAnimation);
	}

	public void closeLayout() {
		this.startAnimation(mCloseAnimation);
	}

	@Override
	public void onAnimationStart(Animation animation) {
		if (animation == mShowAnimation) {
			if (defaultColorSelect) {
				setDefaultSelect(defaultSelect);
				defaultColorSelect = false;
			}
			this.setVisibility(View.VISIBLE);
		}
	}

	@Override
	public void onAnimationEnd(Animation animation) {
		if (animation == mCloseAnimation) {
			this.setVisibility(View.INVISIBLE);
		}
	}

	@Override
	public void onAnimationRepeat(Animation animation) {

	}

	public Animation getOpenAnimation() {
		return mShowAnimation;
	}

	public Animation getCloseAnimation() {
		return mCloseAnimation;
	}

}
